// packageB/serviceOrderList/index.js
import moment from 'moment';
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list: [],
    page: 1,
    resourceURL: wx.api.host + '/',
    status: {
      1: "未派单",
      2: "已派单",
      3: "已完成",
      4: "取消预约",
      5: "已退款",
    },
    end:true
  },
  //请求订单列表
  geiList() {
    wx.http.get(wx.api.orderlist, {
      page: this.data.page,
      limit: 6,
      is_page: 2,
      state: Number(this.data.active) + 1
    }).then(res => {
      if (res.code == 1) {
       
        res.data.forEach((ele) => {
          ele.make_time = this.formatCreateTime(new Date(ele.make_time).getTime()) 
        })
        this.setData({
          list: this.data.list.concat(res.data),
        })
      }
    })
  },
  //转换时间格式
  formatCreateTime(val) {
    return moment(val).calendar(null, {
      sameDay: "MM月DD日 [今天] HH:mm:ss",
      nextDay: "MM月DD日 [明天] HH:mm:ss",
      nextWeek: "MM月DD日  HH:mm:ss",
      lastDay: "MM月DD日 [昨天] HH:mm:ss",
      lastWeek: "MM月DD日  HH:mm:ss",
      sameElse: 'YYYY年MM月DD日  HH:mm:ss'
    });
  },
  //切换导航栏
  onChange(e) {
  
    this.setData({
      page: 1,
      list:[],
      active: e.detail.index
    })
    this.geiList()
  },
  cancel(event) {
    let that = this
    Dialog.confirm({
        confirmButtonColor: "#ff7733",
        cancelButtonColor: "rgba(32,35,39,0.5)",
        message: "确认取消预订吗？",
      })
      .then(() => {
        that.cancelCollect(event.target.dataset.item.send_id);

      })
      .catch(() => {

      });
  },
  cancelCollect(send_id) {

    wx.http.post(wx.api.serviceafter, {
      send_id
    }).then(res => {
      if (res.code == 1) {
        Toast.success(res.msg);
        this.setData({
          page: 1,
          list:[],
          active: e.detail.index
        })
        this.geiList()
      } else {
        Toast.fail(res.msg);
      }

    })


  },
  //添加评论
  toCommon(event) {

    let {send_id,order_id}=event.currentTarget.dataset.item
    wx.navigateTo({
      url: '/packageB/comments/oneComment/index?send_id=' + send_id+'&order_id='+order_id,
    })
  },
  //跳转详情页
  toDetail(event){

    wx.navigateTo({
      url: '/packageB/serviceOrderDetail/index?send_id=' + event.currentTarget.dataset.item.send_id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.active) {
       this.setData({
      active:Number(options.active)
    })
    }
  
    if (this.data.active==0) {
       this.geiList()
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1,
      active: e.detail.index
    })
    this.geiList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})